<template>
  <div class="loginForm" @keyup="keyup">
    <div class="right">
            <span class="title-s">登&nbsp;录</span>
      <span class="title-b"></span>
      <div class="label">
        <input type="text" v-model="username" placeholder="请输入账号或手机号" class="input" />
        <!--        <Icon type="md-person" color="#AAAAAA" size="20" class="picon"/>-->
        <i class="el-icon-user-solid picon"></i>
      </div>
      <div class="label">
        <input :type="isHidden ? 'password' : 'text'" v-model="password" placeholder="请输入密码" class="input" />
        <!--        <Icon type="md-lock" color="#AAAAAA" size="20" class="picon"/>-->
        <i class="el-icon-unlock picon"></i>
<!--                <Icon type="md-eye" color="#AAAAAA" size="20" class="eye" @click="()=>{isHidden=!isHidden}" v-if="!isHidden"/>-->
<!--                <Icon type="md-eye-off" color="#AAAAAA" size="20" class="eye" @click="()=>{isHidden=!isHidden}"-->
<!--                      v-if="isHidden"/>-->
      </div>
      <div class="label">
        <input type="text" v-model="validCode" placeholder="请输入验证码" class="input" />
        <i class="el-icon-mobile-phone picon"></i>
        <!--        <Icon type="logo-css3" color="#AAAAAA" size="20" class="picon"/>-->
        <div class="erweiCode">
          <img :src="imgBase64" @click="getDigitalCaptcha" alt="" />
        </div>
      </div>
      <div class="label">
        <el-checkbox v-model="rememberme">记住密码</el-checkbox>
      </div>
      <div class="submit flex-center" v-loading="loading">
        <span @click="submit">登 录</span>
      </div>
      <div class="label resiger">
        <span  @click="$router.push({name:'fogetPassword'})">忘记密码？</span>
        <span @click="$router.push({name:'resiger'})">免费注册</span>
      </div>
    </div>
      <el-dialog title="修改密码" :visible.sync="passwordShow" width="500px">
        <el-form :model="editForm" ref="editForm"  :rules="rules">
          <el-form-item label="原密码" label-width="100px" prop="oldPassword">
            <el-input v-model="editForm.oldPassword" autocomplete="off" show-password></el-input>
          </el-form-item>
          <el-form-item label="新密码" label-width="100px" prop="newPassword">
            <el-input v-model="editForm.newPassword" autocomplete="off" show-password></el-input>
          </el-form-item>
          <el-form-item label="请重复密码" label-width="100px" prop="verifyPassword">
            <el-input v-model="editForm.verifyPassword" autocomplete="off" show-password></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="passwordShow = false">取 消</el-button>
          <el-button type="primary" @click="setPassword" :loading="loading2">确 定</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>

import JSEncrypt from 'jsencrypt'
export default {
  name: "loginForm",
  data() {
    return {
      imgBase64: "",
      username: "",
      password: "",
      rememberme: false,
      validCode: "",
      loading: false,
      isHidden: true,
      passwordShow: false,
      loading2: false,
      editForm:{
          phone:'',
          email:'',
          oldPassword:'',
          newPassword:'',
          verifyPassword:'',
        },
        rules:{
          phone: [
            { required: true, message: '请输入手机号', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' }
          ],
          oldPassword: [
            { required: true, message: '请输入原密码', trigger: 'blur' }
          ],
          newPassword: [
            { required: true, message: '请输入新密码', trigger: 'blur' }
          ],
          verifyPassword: [
            { required: true, message: '请重复密码', trigger: 'blur' }
          ],
        }
    };
  },
  methods: {
    keyup(e) {
      if (e.keyCode == 13) {
        this.login();
      }
    },
    getDigitalCaptcha() {
      this.$api.getDigitalCaptcha().then((res) => {
        window.biyiCaptchaKey = Object.keys(res)[0];
        this.imgBase64 = res[window.biyiCaptchaKey];
      });
    },
    login() {
      this.loading = true;
      window.biyiCaptcha = this.validCode;
      this.$api.getPublicKey({username:this.username}).then(res=>{
        let jse = new JSEncrypt();
        jse.setPublicKey(res.data)
        let params = {
          username: this.username,
          password: jse.encrypt(this.password),
          validCode: this.validCode,
          data: this.data,
          rememberme: this.rememberme?1:0,
        };
        this.$api
            .login(params)
            .then((res) => {
              this.loading = false;
              this.$store.dispatch("setIsLogin", true);
              localStorage.setItem("token", res.token);
              // if(this.password == 'Jxpg@2023'){
              //   this.forceUpdatePassword()
              //   return
              // }
              this.$api.getUser({}).then((res) => {
                this.$store.dispatch("setUserInfo", res.data);
                localStorage.setItem("userInfo", JSON.stringify(res.data));
                this.$router.push({name:'home'})
              });
            })
            .catch((err) => {
              this.loading = false;
              this.getDigitalCaptcha();
            });
      })

    },
    setPassword(){
        this.$refs['editForm'].validate((valid) => {
          if (valid) {
            if(this.editForm.newPassword != this.editForm.verifyPassword){
              this.$message.warning('两次输入的新密码不一致！')
              return
            }
            let jse1 = new JSEncrypt();
            let jse2 = new JSEncrypt();
            let password = jse2.encrypt(this.editForm.newPassword)
            let params = {
              oldPassword: jse1.encrypt(this.editForm.oldPassword),
              newPassword: password,
              verifyPassword: password,
            }
            this.loading2 = true
            this.$api
            .editpassword(params)
            .then((res) => {
              this.loading2 = false
              this.passwordShow = false
              console.log("res=========", res);
              this.$message.success('修改成功！')
            })
            .catch((e) => {
              console.log(e)
              this.loading2 = false
            });
          }else{
            return false
          }
        })
      },
    forceUpdatePassword(){
      this.$message.warning('请修改初始密码后再登录！正在跳转密码修改页面')
      this.passwordShow = true
    },
    submit() {
      this.login();
    },
  },
  mounted() {
    this.getDigitalCaptcha();
  },
};
</script>

<style scoped lang="less">
/deep/.el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner{
  background-color: #0F47B8;
  border-color: #0F47B8;
}
/deep/.el-checkbox__input.is-checked+.el-checkbox__label{
  color:#0F47B8;
}
.resiger{
  display: flex;
  justify-content: space-between;
  span{
    font-size: 12px;
    color: #666666;
    cursor: pointer;
    &:hover{
      color:#0F47B8;
    }
  }
}
.loginForm {
  width: 410px;
  height: 500px;
  border-radius: 8px;

  display: flex;
  position: relative;
  overflow: hidden;

  .title-s {
    font-size: 30px;
    font-weight: bold;
    color: #333333;
    display: inline-block;
    margin-top: 45px;
  }

  .title-b {
    font-size: 23px;
    color: #333333;
    font-weight: 500;
    display: inline-block;
    margin: 0 0 30px;
  }


  .input {
    outline: none;
    border: 1px solid #EEEEEE;
    border-radius: 4px;
    width: calc(100% - 80px);
    // width: 100%;
    height: 48px;
    padding: 0 30px 0 50px;
    background-color: transparent;

    &:focus {
      border: 1px solid #237ef7;
    }
  }



  .right {
    width: 330px;
    height: 100%;
    padding: 0 40px;
    background-color: white;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: rgba(255,255,255,0.95);
    .title {
      display: inline-block;
      font-size: 21px;
      font-weight: 500;
      margin-top: 40px;
    }

    .anotherLogin {
      img {
        border-radius: 100%;
        width: 50px;
        height: 50px;
        cursor: pointer;
      }
    }

    .label {
      width: 100%;
      padding: 15px 20px 0;
      position: relative;
      i{
        font-size: 20px;
        color: #AAAAAA ;
      }

      .picon {
        position: absolute;
        top: 28px;
        left: 30px;
      }

      .eye {
        position: absolute;
        top: 38px;
        right: 20px;
        cursor: pointer;
      }

      .erweiCode {
        width: 120px;
        height: 47px;
        position: absolute;
        top: 16px;
        right: 20px;
        cursor: pointer;

        img {
          width: 100%;
          height: 100%;
        }
      }
    }

    .submit {

      margin-top: 30px;
      width: 100%;
      height: 50px;
      background: #0B4AC0;
      box-shadow: 0px 5px 7px 0px rgba(24, 122, 251, 0.36);
      border-radius: 4px;
      color: white;
      font-size: 18px;
      font-weight: 500;
      letter-spacing: 10px;
      cursor: pointer;
      position: relative;

      &:hover {
        box-shadow: none;
      }

      span {
        display: inline-block;
        width: 100%;
        height: 100%;
        text-align: center;
        line-height: 50px;
      }
    }
  }
}


</style>
